﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using DomainClasses;

namespace Pluralsight.DataLayer
{
	/*
	 Comando para Habilitar o Migrations
	 * Enable-Migrations -EnableAutomaticMigrations
	 
	 */
	public class Context : DbContext
	{
		public DbSet<Blog> Blogs
		{
			get;
			set;
		}

		public DbSet<Post> Posts
		{
			get;
			set;
		}

		protected override void OnModelCreating( DbModelBuilder modelBuilder )
		{
			modelBuilder.Entity<Blog>().HasKey( b => b.Id ).Property( b => b.Title ).HasMaxLength( 20 );
			modelBuilder.Entity<Blog>().Property( b => b.BloggerName ).IsRequired();
			base.OnModelCreating( modelBuilder );
		}

	}

	public class MyInitializer : DropCreateDatabaseAlways<Context>
	{
		protected override void Seed( Context context )
		{
			new List<Blog> {	new Blog {	BloggerName = "Julie", 
													Title = "My Code First Blog", 
													DateCreated = new DateTime( 2011, 4, 1 ), 
													Posts = new List<Post> { new Post	{	Title = "DB Initialization Annotation", 
																										Content = "Mark navigation property with ForeignKey" 
																									} 
																					} } }.ForEach( b => context.Blogs.Add( b ) );

			base.Seed( context );
		}
	}
}
